var express = require("express");
var router = express.Router();
var bug = require("../service/Program/index");
let jwt = require("jsonwebtoken");
let userService = require("../service/userService");
//引入一个文件夹  默认就是引入该文件夹中的index文件





//分页------------------------------------------------------------
router.get("/query", function (req, res, next) {
  //接收前端传来的数据  写入数据库  返回值
  let token = req.headers.token;
  jwt.verify(token, "abc", async (err, info) => {
      
    if (err) {
      res.json({
        code: 0,
        msg: "token解析异常，请重新登录",
      });
    } else {
      if (info.type == 3) {
        let { page, shownum, key, type } = req.query; //接受参数 get请求用query
        let params = {};
        params.DeveloperName=info.username
       
        let skipnum = (page-1)*shownum;
        let reg = new RegExp(key);
        params.bugreason = reg; //精确查询
        if (type) {
          params.type = type * 1;
        }
      
        let count = await bug.count(params);//获取数据总数
        let list = await bug.query(params,skipnum * 1, shownum * 1);
        res.json({
          code: 1,
          msg: "获取",
          data: list,
          count:count
        });
      } else {
        res.json({
          code: 0,
          msg: "用户无权限进行此操作",
        });
      }
    }
  });
});





//更改BUG状态
router.patch("/update", (req, res, next) => {
  
  let { _id, type } = req.body;
  let token = req.headers.token;
  jwt.verify(token, "abc", async function (err, code) {
      
    if (!err) {
      if (code.type == 3) {
        let data = await bug.update({_id}, { $set: { type } });
        res.json({
          code: 1,
          msg: data,
        });
      } else {
        res.json({
          code: 0,
          msg: "权限不够",
        });
      }
    } else {
      res.json({
        code: 0,
        msg: "未登录" + err,
      });
    }
  });
});





module.exports = router;
